﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Xml.Linq;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web.Mvc;
using COM.Service;
using COM.Web;
using WebApp.Service;
using COM.Data;

namespace WebApp.Web
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class WcfSystemService : IWcfSystemService
    {
        private IPermissionService permissionService = Unity.Instance.GetService<IPermissionService>();
        
        [WebInvoke]
        public DateTime GetServerDateTime()
        {
            return DateTime.Now;
        }

        /// <summary>
        /// AndroidApp最新的版本号,建议下载
        /// </summary>
        /// <returns></returns>
        [WebInvoke]
        public string GetAndroidAppNewestVer()
        {
            var configService = Unity.Instance.GetService<IWebConfigService>();
            var xml = configService.GetAppConfigXml("AppVer", isCache: false);
            return xml.Element("AndroidApp").Attribute("NewVer").Value;
        }

        /// <summary>
        /// 兼容的最旧版本号,早于此版本的必须升级,无关键更新保留此版本号
        /// </summary>
        /// <returns></returns>
        [WebInvoke]
        public string GetAndroidAppOldestVer()
        {
            var  configService = Unity.Instance.GetService<IWebConfigService>();
            var xml = configService.GetAppConfigXml("AppVer",isCache:false);
            return xml.Element("AndroidApp").Attribute("OldVer").Value;
        }


        /// <summary>
        /// 兼容的最旧版本号,早于此版本的必须升级,无关键更新保留此版本号
        /// </summary>
        /// <returns></returns>
        [WebInvoke]
        public string GetAndroidAppNewestUrl()
        {
            string androidAppNewestUrl = "Default/Home/DownloadZip";
            return string.Format("http://{0}/{1}", Unity.Instance.GetService<IConfigService>().DeployHostName, androidAppNewestUrl);
        }

        [WebInvoke]
        public void CreateUserFeedback(Guid userID, string app,string  ver,string  userInfo,string  userContent)
        {
            FileLogService.Instance.Log("收到反馈");
            var userPrincipalFactory = COM.Web.Unity.Instance.GetService<IUserPrincipalFactory>();
            System.Web.HttpContext.Current.User = userPrincipalFactory.CreateMvcUserPrincipal(userID, "Anonymous");
            var userFeedbackService = Unity.Instance.GetService<IUserFeedbackService>();
            userFeedbackService.CreateUserFeedback(app, ver, userID, userInfo, userContent);
        }
    }
}
